package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/admin/dashboard")
public class DashboardController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsProductService goodsProductService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CategoryService categoryService;

/**
 * "data":{
 *         "goodsTotal":426,
 *         "userTotal":197,
 *         "productTotal":8939,
 *         "orderTotal":107
 *     },*/
    @RequiresUser
    @GetMapping
    public Object dashboard(){
        Map<String,Long> map=new HashMap();
        map.put("goodsTotal",goodsService.findGoodsCount());
        map.put("userTotal",userService.findUsersCount());
        map.put("productTotal",goodsProductService.findProductsCount());
        map.put("orderTotal",orderService.findOrdersCount());


        return ResponseUtil.ok(map);
    }


    /**
     * "categoryNames":["数码家电","日用百货","鞋帽箱包"],
     *             "categorySellData":[
     *                 {"name":"数码家电","value":68.76},
     *                 {"name":"日用百货","value":48.76},
     *                 {"name":"鞋帽箱包","value":277.53}
     *             ]
     */
    @RequiresUser
    @GetMapping("/chart")
    public Object chart(){
        List<CategorySellAmts> categoryAndTotal = categoryService.findCategoryAndTotal();

        CategorySellVo categorySellVo=new CategorySellVo();

        String[] strings = categoryAndTotal.stream().map(CategorySellAmts::getName)
                .collect(Collectors.toList()).toArray(new String[categoryAndTotal.size()]);
        categorySellVo.setCategoryNames(strings);
        categorySellVo.setCategorySellData(categoryAndTotal);


        OrderAmtsVo orderAmtsVo=new OrderAmtsVo();
        List<DayStatis> dayStatisList = orderService.findDayStatis();
        Integer[] integers = dayStatisList.stream().map(DayStatis::getCnts)
                .collect(Collectors.toList()).toArray(new Integer[dayStatisList.size()]);
        orderAmtsVo.setOrderCntData(integers);
        String[] str = dayStatisList.stream().map(DayStatis::getDayStr)
                .collect(Collectors.toList()).toArray(new String[dayStatisList.size()]);
        orderAmtsVo.setDayData(str);
        List<BigDecimal> collect = dayStatisList.stream().map(DayStatis::getAmts)
                .collect(Collectors.toList());
        orderAmtsVo.setOrderAmtData(collect);

        List<DayStatis> dayStatisListUser=userService.findDayStatis();
        UserOrderCntVo userOrderCntVo=new UserOrderCntVo();
        String[] dates = fetchDayData(dayStatisList, dayStatisListUser);


        Integer[] orderCnt = fetchCnt(dates, dayStatisList);
        Integer[] userCnt = fetchCnt(dates, dayStatisListUser);
        userOrderCntVo.setDayData(dates);
        userOrderCntVo.setUserCnt(userCnt);
        userOrderCntVo.setOrderCnt(orderCnt);

        Map map=new HashMap();
        map.put("categorySell",categorySellVo);
        map.put("orderAmts",orderAmtsVo);
        map.put("userOrderCnt",userOrderCntVo);


        return ResponseUtil.ok(map);
    }













    private Integer[] fetchCnt(String[] dates, List<DayStatis> dayStatisList) {

        List<Integer> cnts=new ArrayList<>();

        Map<String,Integer> dayMap=dayStatisList.stream()
                .collect(Collectors.toMap(DayStatis::getDayStr,DayStatis::getCnts));

        Arrays.stream(dates).forEach(day->{
            Integer cnt=dayMap.get(day);
            if (cnt==null){
                cnts.add(0);
            }else {
                cnts.add(cnt);
            }
        });

        return cnts.toArray(new Integer[cnts.size()]);



    }

    private String[] fetchDayData(List<DayStatis> dayStatisList, List<DayStatis> dayStatisListUser) {

        Set<String> set = new HashSet<>();
        dayStatisList.forEach(dayStatis -> {
            Optional.ofNullable(dayStatis.getDayStr()).ifPresent(set::add);
        });
        dayStatisListUser.forEach(dayStatis -> {
            Optional.ofNullable(dayStatis.getDayStr()).ifPresent(set::add);
        });

        //Lambda排序
        return set.stream().sorted((s1,s2) -> s1.compareTo(s2)
        ).collect(Collectors.toList()).toArray(new String[set.size()]);


    }

}
